<?php
/**
 * @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
 * @license https://opensource.org/licenses/GPL-2.0
 * @link https://www.koogua.com
 */

namespace App\Http\Admin\Services;

use App\Builders\AgencyList;
use App\Builders\ArticleList as ArticleListBuilder;
use App\Builders\ReportList as ReportListBuilder;
use App\Caches\Article as ArticleCache;
use App\Exceptions\BadRequest as BadRequestException;
use App\Library\Paginator\Query as PagerQuery;
use App\Library\Utils\Word as WordUtil;
use App\Models\Article as ArticleModel;
use App\Models\Category as CategoryModel;
use App\Models\Reason as ReasonModel;
use App\Models\Report as ReportModel;
use App\Models\User as UserModel;
use App\Repos\Article as ArticleRepo;
use App\Repos\Category as CategoryRepo;
use App\Repos\Report as ReportRepo;
use App\Repos\User as UserRepo;
use App\Services\Logic\Article\ArticleDataTrait;
use App\Services\Logic\Article\ArticleInfo as ArticleInfoService;
use App\Services\Logic\Article\XmTagList as XmTagListService;
use App\Services\Logic\Notice\Internal\ArticleApproved as ArticleApprovedNotice;
use App\Services\Logic\Notice\Internal\ArticleRejected as ArticleRejectedNotice;
use App\Services\Logic\Point\History\ArticlePost as ArticlePostPointHistory;
use App\Services\Sync\ArticleIndex as ArticleIndexSync;
use App\Validators\Article as ArticleValidator;

class Agency extends Service
{

    public function getAgencies()
    {
        $pagerQuery = new PagerQuery();

        $params = $pagerQuery->getParams();

        $params['deleted'] = $params['deleted'] ?? 0;

        $sort = $pagerQuery->getSort();
        $page = $pagerQuery->getPage();
        $limit = $pagerQuery->getLimit();

        $articleRepo = new \App\Repos\Agency();

        $pager = $articleRepo->paginate($params, $sort, $page, $limit);

        return $this->handleAgencies($pager);
    }

    protected function handleAgencies($pager)
    {
        if ($pager->total_items > 0) {

            $items = $pager->items->toArray();
            $pager->items = $items;
        }

        return $pager;
    }

    public function getAgency($id)
    {
        $articleRepo = new \App\Repos\Agency();

        $article = $articleRepo->findById($id);

        if (!$article) {
            throw new BadRequestException('article.not_found');
        }

        return $article;
    }


    public function createAgency()
    {
        $post = $this->request->getPost();
        $title = $post['title'];
        $content = $post['content'];
        $cover = $post['cover'];

        $article = new \App\Models\Agency();

        $article->title = $title;
        $article->content = $post['content'];
        $article->cover = $cover;
        $article->to_index = $post['to_index'];
        $article->create();

        return $article;
    }

    public function updateAgency($id)
    {
        $post = $this->request->getPost();
        $articleRepo = new \App\Repos\Agency();
        $article = $articleRepo->findById($id);

        $article->title = $post['title'];
        $article->cover = $post['cover'];
        $article->content = $post['content'];
        $article->to_index = $post['to_index'];
        $article->update();

        print_r($article->to_index);exit;
        return $article;
    }

    public function deleteAgency($id)
    {
        $articleRepo = new \App\Repos\Agency();
        $article = $articleRepo->findById($id);
        $article->deleted = 1;
        $article->update();
        return $article;
    }

}
